package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.PositionQueryDto;
import com.group.eams.pojo.entity.Position;
import com.group.eams.pojo.entity.Position;
import com.group.eams.service.PositionService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 岗位管理(Position)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/position")
public class PositionController  {
    /**
     * 服务对象
     */
    @Autowired
    private PositionService positionService;
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "岗位列表",httpMethod = "GET")
    public Result list(@RequestBody PositionQueryDto positionQueryDto){

        //1.准备分页条件
        Page<Position> page=new Page<>(positionQueryDto.getPage(),positionQueryDto.getLimit());

        //2.设置查询条件
        LambdaQueryWrapper<Position> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(!StrUtil.isEmpty(positionQueryDto.getName()),
                        Position::getName,positionQueryDto.getName()).
                eq(!StrUtil.isEmpty(positionQueryDto.getCode()),Position::getCode,
                        positionQueryDto.getCode()).
                eq(positionQueryDto.getStatus()!=null,Position::getStatus,positionQueryDto.getStatus());

        //3.查询分页数据
        Page<Position> positionPage = positionService.page(page, lambdaQueryWrapper);

        //返回结果
        return Result.buildSuccess(positionPage.getTotal(),positionPage.getRecords());
    }

    @RequestMapping(value = "/find",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找岗位")
    public Result find(@RequestParam Long id){
        Position position = positionService.getById(id);
        return Result.buildSuccess(position);
    }
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "查找所有岗位")
    public Result findAll(){
        List<Position> positionList = positionService.list();
        return Result.buildSuccess(positionList);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加岗位")
    public Result add(@RequestBody Position position){
        boolean result = positionService.save(position);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation("修改岗位")
    public Result update(@RequestBody Position position){
        boolean result = positionService.updateById(position);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ApiOperation("根据id删除岗位")
    public Result delete(@RequestParam Long id){
        boolean result = positionService.removeById(id);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    @ApiOperation("批量删除岗位")
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = positionService.removeByIds(batchDeleteDto.getIds());
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

}


